﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.BL;
using System.Data;
using MandalaySoft.Common;

public partial class Grade_GradeScaleDetail : System.Web.UI.Page
{
    MyError _error = new MyError();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[Global.Session_SchoolID] == null)
        {
            Response.Redirect("~/Default.aspx");
        }

        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        GradeScaleDetailDB _db = new GradeScaleDetailDB();
        DataTable _dt = new DataTable("tblGradeScale");
        _error = new MyError();
        _dt = _db.GetGradeScaleDetailList(Convert.ToInt32(Session[Global.Session_GradeScaleID].ToString()), out _error);

        gvGradeScaleDetail.DataSource = _dt;
        gvGradeScaleDetail.DataBind();
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        BindData();
        gvGradeScaleDetail.PageIndex = e.NewPageIndex;
        gvGradeScaleDetail.DataBind();
    }

    protected void AddGradeScaleDetail(object sender, EventArgs e)
    {
        string GradeScaleDetailID = ((TextBox)gvGradeScaleDetail.FooterRow.FindControl("txtGradeScaleDetailID")).Text;
        string GradeScaleTitle = ((TextBox)gvGradeScaleDetail.FooterRow.FindControl("txtTitle")).Text;
        string BreakOff = ((TextBox)gvGradeScaleDetail.FooterRow.FindControl("txtBreakOff")).Text;
        string WeightedGPA = ((TextBox)gvGradeScaleDetail.FooterRow.FindControl("txtWeightedGPA")).Text;
        string UnWeightedGPA = ((TextBox)gvGradeScaleDetail.FooterRow.FindControl("txtUnWeightedGPA")).Text;
        string SortOrder = ((TextBox)gvGradeScaleDetail.FooterRow.FindControl("txtSortOrder")).Text;
        string Comment = ((TextBox)gvGradeScaleDetail.FooterRow.FindControl("txtComment")).Text;
        bool IsActive = ((CheckBox)gvGradeScaleDetail.FooterRow.FindControl("chkActive")).Checked;

        if (SaveGradeScaleDetail(GradeScaleDetailID, GradeScaleTitle, BreakOff, WeightedGPA, UnWeightedGPA, SortOrder, Comment, IsActive) > 0)
        {
            BindData();
        }
    }

    private int SaveGradeScaleDetail(string GradeScaleDetailID, string GradeScaleTitle, string BreakOff, string WeightedGPA, string UnWeightedGPA, string SortOrder, string Comment, bool IsActive)
    {
        GradeScaleDetail _item = new GradeScaleDetail();
        _item.GradeScaleDetailID = Convert.ToInt32(GradeScaleDetailID);
        _item.GradeScaleID = Convert.ToInt32(Session[Global.Session_GradeScaleID].ToString());
        _item.Title = GradeScaleTitle;
        _item.BreakOff = Convert.ToDecimal(BreakOff);
        _item.WeightedGPA = Convert.ToDecimal(WeightedGPA);
        _item.UnWeightedGPA = Convert.ToDecimal(UnWeightedGPA);
        _item.SortOrder = Convert.ToInt16(SortOrder);
        _item.Comment = Comment;
        _item.IsActive = IsActive;

        GradeScaleDetailDB _db = new GradeScaleDetailDB();
        _item = _db.Save(_item);

        return _item.GradeScaleID;
    }

    protected void UpdateGradeScaleDetail(object sender, GridViewUpdateEventArgs e)
    {
        string GradeScaleDetailID = ((Label)gvGradeScaleDetail.Rows[e.RowIndex].FindControl("lblGradeScaleDetailID")).Text;
        string GradeScaleTitle = ((TextBox)gvGradeScaleDetail.Rows[e.RowIndex].FindControl("txtTitle")).Text;
        string BreakOff = ((TextBox)gvGradeScaleDetail.Rows[e.RowIndex].FindControl("txtBreakOff")).Text;
        string WeightedGPA = ((TextBox)gvGradeScaleDetail.Rows[e.RowIndex].FindControl("txtWeightedGPA")).Text;
        string UnWeightedGPA = ((TextBox)gvGradeScaleDetail.Rows[e.RowIndex].FindControl("txtUnWeightedGPA")).Text;
        string SortOrder = ((TextBox)gvGradeScaleDetail.Rows[e.RowIndex].FindControl("txtSortOrder")).Text;
        string Comment = ((TextBox)gvGradeScaleDetail.Rows[e.RowIndex].FindControl("txtComment")).Text;
        bool IsActive = ((CheckBox)gvGradeScaleDetail.Rows[e.RowIndex].FindControl("chkActive")).Checked;

        if (SaveGradeScaleDetail(GradeScaleDetailID, GradeScaleTitle, BreakOff, WeightedGPA, UnWeightedGPA, SortOrder, Comment, IsActive) > 0)
        {
            gvGradeScaleDetail.EditIndex = -1;
            BindData();
        }
    }

    protected void DeleteGradeScaleDetail(object sender, EventArgs e)
    {
        LinkButton lnkRemove = (LinkButton)sender;
        GradeScaleDetailDB _db = new GradeScaleDetailDB();
        _error = new MyError();
        _db.Delete(Convert.ToInt32(lnkRemove.CommandArgument), out _error);
        if (_error.ErrorNo == 0)
        {
            BindData();
        }
    }

    protected void EditGradeScaleDetail(object sender, GridViewEditEventArgs e)
    {
        gvGradeScaleDetail.EditIndex = e.NewEditIndex;
        //CheckBox chkCalculateGPA = (CheckBox)gvGradeScaleDetail.Rows[e.NewEditIndex].FindControl("chkCalculateGPA");
        //Label lblCalculateGPA = (Label)gvGradeScaleDetail.Rows[e.NewEditIndex].FindControl("lblCalculateGPA");

        //if (chkCalculateGPA != null && lblCalculateGPA != null)
        //{
        //    if (lblCalculateGPA.Text.ToUpper() == "YES")
        //    {
        //        chkCalculateGPA.Checked = true;
        //    }
        //    else
        //    {
        //        chkCalculateGPA.Checked = false;
        //    }
        //}
        BindData();
    }

    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvGradeScaleDetail.EditIndex = -1;
        BindData();
    }

    protected void gvGradeScaleDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (drv != null)
            {
                Label lblActive = (Label)e.Row.FindControl("lblActive");
                CheckBox chkActive = (CheckBox)e.Row.FindControl("chkActive");
                if (lblActive != null)
                {
                    if (drv["IsActive"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
                        {
                            lblActive.Text = "Yes";
                        }
                        else
                        {
                            lblActive.Text = "No";
                        }
                    }
                }

                if (chkActive != null)
                {
                    if (drv["IsActive"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
                        {
                            chkActive.Checked = true;
                        }
                        else
                        {
                            chkActive.Checked = false;
                        }
                    }
                }
            }
        }
    }

    protected void gvGradeScaleDetail_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //if ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)))
        //{
        //    DataRowView drv = (DataRowView)e.Row.DataItem;

        //    if (drv != null)
        //    {
        //        CheckBox chkCalculateGPA = (CheckBox)e.Row.FindControl("chkCalculateGPA");
        //        CheckBox chkIgnoreSchedule = (CheckBox)e.Row.FindControl("chkIgnoreSchedule");
        //        CheckBox chkActive = (CheckBox)e.Row.FindControl("chkActive");

        //        if (chkCalculateGPA != null)
        //        {
        //            if (drv["IsCalculateGPA"] != null)
        //            {
        //                if (Convert.ToBoolean(drv["IsCalculateGPA"].ToString()) == true)
        //                {
        //                    chkCalculateGPA.Checked = true;
        //                }
        //                else
        //                {
        //                    chkCalculateGPA.Checked = false;
        //                }
        //            }
        //        }


        //        if (chkIgnoreSchedule != null)
        //        {
        //            if (drv["IsIgnoreSchedule"] != null)
        //            {
        //                if (Convert.ToBoolean(drv["IsIgnoreSchedule"].ToString()) == true)
        //                {
        //                    chkIgnoreSchedule.Checked = true;
        //                }
        //                else
        //                {
        //                    chkIgnoreSchedule.Checked = false;
        //                }
        //            }
        //        }


        //        if (chkActive != null)
        //        {
        //            if (drv["IsActive"] != null)
        //            {
        //                if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
        //                {
        //                    chkActive.Checked = true;
        //                }
        //                else
        //                {
        //                    chkActive.Checked = false;
        //                }
        //            }
        //        }
        //    }
        //}
    }
}